Conditions | 4 |
Paths | 4 |
Total Lines | 121 |
Code Lines | 66 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | // Copyright Joyent, Inc. and other Node contributors. |
||
183 | exports.realpath = function realpath(p, cache, cb) { |
||
184 | if (typeof cb !== 'function') { |
||
185 | cb = maybeCallback(cache); |
||
186 | cache = null; |
||
187 | } |
||
188 | |||
189 | // make p is absolute |
||
190 | p = pathModule.resolve(p); |
||
191 | |||
192 | if (cache && Object.prototype.hasOwnProperty.call(cache, p)) { |
||
193 | return process.nextTick(cb.bind(null, null, cache[p])); |
||
194 | } |
||
195 | |||
196 | var original = p, |
||
197 | seenLinks = {}, |
||
198 | knownHard = {}; |
||
199 | |||
200 | // current character position in p |
||
201 | var pos; |
||
202 | // the partial path so far, including a trailing slash if any |
||
203 | var current; |
||
204 | // the partial path without a trailing slash (except when pointing at a root) |
||
205 | var base; |
||
206 | // the partial path scanned in the previous round, with slash |
||
207 | var previous; |
||
208 | |||
209 | start(); |
||
210 | |||
211 | function start() { |
||
212 | // Skip over roots |
||
213 | var m = splitRootRe.exec(p); |
||
214 | pos = m[0].length; |
||
215 | current = m[0]; |
||
216 | base = m[0]; |
||
217 | previous = ''; |
||
218 | |||
219 | // On windows, check that the root exists. On unix there is no need. |
||
220 | if (isWindows && !knownHard[base]) { |
||
221 | fs.lstat(base, function(err) { |
||
222 | if (err) return cb(err); |
||
223 | knownHard[base] = true; |
||
224 | LOOP(); |
||
225 | }); |
||
226 | } else { |
||
227 | process.nextTick(LOOP); |
||
228 | } |
||
229 | } |
||
230 | |||
231 | // walk down the path, swapping out linked pathparts for their real |
||
232 | // values |
||
233 | function LOOP() { |
||
234 | // stop if scanned past end of path |
||
235 | if (pos >= p.length) { |
||
236 | if (cache) cache[original] = p; |
||
237 | return cb(null, p); |
||
238 | } |
||
239 | |||
240 | // find the next part |
||
241 | nextPartRe.lastIndex = pos; |
||
242 | var result = nextPartRe.exec(p); |
||
243 | previous = current; |
||
244 | current += result[0]; |
||
245 | base = previous + result[1]; |
||
246 | pos = nextPartRe.lastIndex; |
||
247 | |||
248 | // continue if not a symlink |
||
249 | if (knownHard[base] || (cache && cache[base] === base)) { |
||
250 | return process.nextTick(LOOP); |
||
251 | } |
||
252 | |||
253 | if (cache && Object.prototype.hasOwnProperty.call(cache, base)) { |
||
254 | // known symbolic link. no need to stat again. |
||
255 | return gotResolvedLink(cache[base]); |
||
256 | } |
||
257 | |||
258 | return fs.lstat(base, gotStat); |
||
259 | } |
||
260 | |||
261 | function gotStat(err, stat) { |
||
262 | if (err) return cb(err); |
||
263 | |||
264 | // if not a symlink, skip to the next path part |
||
265 | if (!stat.isSymbolicLink()) { |
||
266 | knownHard[base] = true; |
||
267 | if (cache) cache[base] = base; |
||
268 | return process.nextTick(LOOP); |
||
269 | } |
||
270 | |||
271 | // stat & read the link if not read before |
||
272 | // call gotTarget as soon as the link target is known |
||
273 | // dev/ino always return 0 on windows, so skip the check. |
||
274 | if (!isWindows) { |
||
275 | var id = stat.dev.toString(32) + ':' + stat.ino.toString(32); |
||
276 | if (seenLinks.hasOwnProperty(id)) { |
||
277 | return gotTarget(null, seenLinks[id], base); |
||
278 | } |
||
279 | } |
||
280 | fs.stat(base, function(err) { |
||
281 | if (err) return cb(err); |
||
282 | |||
283 | fs.readlink(base, function(err, target) { |
||
284 | if (!isWindows) seenLinks[id] = target; |
||
285 | gotTarget(err, target); |
||
286 | }); |
||
287 | }); |
||
288 | } |
||
289 | |||
290 | function gotTarget(err, target, base) { |
||
291 | if (err) return cb(err); |
||
292 | |||
293 | var resolvedLink = pathModule.resolve(previous, target); |
||
294 | if (cache) cache[base] = resolvedLink; |
||
295 | gotResolvedLink(resolvedLink); |
||
296 | } |
||
297 | |||
298 | function gotResolvedLink(resolvedLink) { |
||
299 | // resolve the link, then start over |
||
300 | p = pathModule.resolve(resolvedLink, p.slice(pos)); |
||
301 | start(); |
||
302 | } |
||
303 | }; |
||
304 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.